const loggerDecorator = () => {
  return function logMethod(
    target: any,
    propertyName: string,
    propertyDescriptor: any
  ) {
    console.log(target[propertyName]);

    const method = target[propertyName];

    // 重载方法
    target[propertyName] = function async(...args: any[]) {
      try {
        return method.apply(this, args); // 调用之前的函数
      } finally {
        const now = new Date().valueOf();
        console.log(`lasted logged in ${now}`);
      }
    };
    return propertyDescriptor;
  };
};

class UserService {
  @loggerDecorator()
  login() {
    console.log("login success");
  }
}

const exp6 = () => {
  console.log();
  console.log("-----------------------示例6:方法装饰器-----------------------");
  console.log(
    "-----------------------使用装饰器重写示例1-----------------------"
  );
  console.log();
  const user = new UserService();
  user.login();
  console.log();
  console.log("-----------------------示例6:执行完毕-----------------------");
};
exp6();
